import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;


public class Attendance {

    /**
     * 当前登录的用户信息
     */
    private static HashMap<String,Object> currentUserInfo = new HashMap<>();

    /**
     * 获取当前登录用户的类型
     * @return
     */
    private static int getCurrentUserType(){
        return Integer.parseInt(currentUserInfo.get("userType").toString());
    }

    /**
     * 获取当前登录的用户名
     * @return
     */
    private static String getCurrentUserName(){
        return currentUserInfo.get("userName").toString();
    }

    /**
     * 存储当前用户信息
     * @param userName
     * @param password
     * @param userType
     */
    private static void setCurrentUserInfo(String userName,String password,int userType){
        currentUserInfo.put("userName",userName);
        currentUserInfo.put("password",password);
        currentUserInfo.put("userType",userType);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎使用企业考勤管理系统!");
//        清空用户信息
        currentUserInfo = new HashMap<>();
        login(scanner);
    }

    /**
     * 用户登录（进入用户登录界面）
     * @param scanner
     */
    private static void login(Scanner scanner){
        System.out.println("请选择用户类型(输入数字)!");
        System.out.println("1.管理员登录;2.普通用户登录;3.退出系统");
        int userType = scanner.nextInt();
        if (userType == 3){
            System.exit(0);
        }
        System.out.println("请输入用户名");
        String userName = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
//        检查用户名和密码是否正确
        boolean loginResult = checkLogin(userName, password,userType);
        if (loginResult){
//            存储用户信息
            setCurrentUserInfo(userName,password,userType);
            menuSelect(scanner);
        }else{
            System.err.println("用户名或密码错误!");
            login(scanner);
        }
    }

    /**
     * 用户登录-匹配文件中存储的用户信息
     * @param userName 用户名
     * @param password 密码
     * @param userType 用户类型
     * @return
     */
    private static boolean checkLogin(String userName,String password,int userType){
//       读取文件
        String fileName = "user.txt";
        FileReader fileReader = null;
        BufferedReader bf = null;
        boolean result = false;
        try{
//            从文本文件中读取数据
            File file = new File(fileName);
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return result;
            }
            fileReader = new FileReader(fileName);
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                if (userName.equals(lines[1])){
                    if (password.equals(lines[2])){
                        result = true;
                    }
                    break;
                }
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }



    /**
     * 菜单选择
     * @param scanner 键盘输入
     */
    private static void menuSelect(Scanner scanner){
        System.out.println("请选择功能(输入数字)!");
        if (getCurrentUserType() == 1){
            System.out.println("1.修改密码;2.用户管理;3.工作时间管理;4.考勤统计;5.退出系统");
            //        根据用户输入选择功能
            int functionType = scanner.nextInt();
            switch (functionType){
                case 1:
                    accountSettingMenu(scanner);
                    break;
                case 2:
                    userMenu(scanner);
                    break;
                case 3:
                    workTimeSettingMenu(scanner);
                    break;
                case 4:
                    queryattendanceMenu(scanner);
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:break;
            }
        }else{
            System.out.println("1.考勤打卡;2.我的考勤记录;5.退出系统");
            //        根据用户输入选择功能
            int functionType = scanner.nextInt();
            switch (functionType){
                case 1:
                    attendanceMenu(scanner);
                    break;
                case 2:
                    myAttendanceMenu(scanner);
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:break;
            }
        }
    }


    private static void accountSettingMenu(Scanner scanner){
        System.out.println("请输入原始密码");
        String oldPwd = scanner.next();
        System.out.println("请输入新密码");
        String newPwd = scanner.next();
        boolean b = accountSetting(oldPwd, newPwd);
        if (b){
            System.out.println("修改成功，请重新登录");
            login(scanner);
        }else{
            System.out.println("修改失败");
            accountSettingMenu(scanner);
        }
    }

    /**
     * 修改密码
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @return
     */
    private static boolean accountSetting(String oldPwd,String newPwd){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("user.txt");
        File file1 = new File("user_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        boolean result = false;
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("user.txt");
            fileWriter = new FileWriter("user_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
//                找到要修改的数据,修改数据
                if (getCurrentUserType() == Integer.parseInt(lines[0])){
                    if (getCurrentUserName().equals(lines[1])){
//                        原始密码匹配上了
                        if (oldPwd.equals(lines[2])){
//                            重新生成写入的数据，即修改密码
                            line = lines[0]+";"+lines[1]+";"+newPwd;
                            result = true;
                        }else{
                            result = false;
                        }
                    }
                }
                bw.write(line);
                bw.newLine();
            }
            return result;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("user.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }

    private static void userMenu(Scanner scanner){
        System.out.println("全部用户信息如下");
        List<HashMap<String, String>> hashMaps = queryUserList();
        for (HashMap<String, String> hashMap : hashMaps) {
            System.out.println("用户类型:"+hashMap.get("userType")+";用户名:"+hashMap.get("userName")+";用户密码:"+hashMap.get("password")
                    +";性别:"+hashMap.get("gender")+";学历:"+hashMap.get("education"));
        }
        System.out.println("1.新增用户;2.修改用户;3.删除用户;4.返回上一层菜单;5.退出系统");
        int type = scanner.nextInt();
        switch (type){
            case 1: addUserMenu(scanner);break;
            case 2: updateUserMenu(scanner);break;
            case 3: deleteUserMenu(scanner);break;
            case 4: menuSelect(scanner);break;
            case 5: System.exit(0);
            default:
        }
    }


    /**
     * 查询所有用户
     * @return
     */
    private static List<HashMap<String,String>> queryUserList(){
        FileReader fileReader = null;
        BufferedReader bf = null;
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            从文本文件中读取数据
            File file = new File("user.txt");
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("user.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                if (line == ""){
                    continue;
                }
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                item.put("userType",Integer.parseInt(lines[0])==1?"管理员":"普通用户");
                item.put("userName",lines[1]);
                item.put("password",lines[2]);
                item.put("gender",lines[3]);
                item.put("education",lines[4]);
                data.add(item);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }

    private static void addUserMenu(Scanner scanner){
        System.out.println("请输入用户名");
        String userName = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        System.out.println("请输入用户类型(1:管理员;2:普通用户)");
        int userType = scanner.nextInt();
        System.out.println("请输入性别");
        String gender = scanner.next();
        System.out.println("请输入学历");
        String education = scanner.next();
        boolean result = addUser(userName,password,userType,gender,education);
        if (result){
            System.out.println("新增成功");
        }else{
            System.err.println("新增失败");
        }
        userMenu(scanner);
    }

    private static void updateUserMenu(Scanner scanner){
        System.out.println("请输入用户名");
        String userName = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        System.out.println("请输入用户类型(1:管理员;2:普通用户)");
        int userType = scanner.nextInt();
        System.out.println("请输入性别");
        String gender = scanner.next();
        System.out.println("请输入学历");
        String education = scanner.next();
        boolean result = updateUser(userName,password,userType,gender,education);
        if (result){
            System.out.println("修改成功");
        }else{
            System.err.println("修改失败");
        }
        userMenu(scanner);
    }

    private static void deleteUserMenu(Scanner scanner){
        System.out.println("请输入用户名");
        String userName = scanner.next();
        boolean result = deleteUser(userName);
        if (result){
            System.out.println("删除成功");
        }else{
            System.err.println("删除失败");
        }
        userMenu(scanner);
    }

    /**
     * 新增用户
     * @param userName 用户名
     * @param password 密码
     * @param userType 用户类型
     */
    private static boolean addUser(String userName,String password,int userType,String gender,String education){
        FileWriter filew = null;
        BufferedWriter bw = null;
        boolean flag = false;
        try {
//            写入文件
            File file = new File("user.txt");
            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            filew = new FileWriter(file,true);
            bw = new BufferedWriter(filew);
            if (file.length()==0){
                flag = true;
            }
            String info = userType+";"+userName+";"+password+";"+gender+";"+education;
//            是否换行
            if (!flag){
                bw.newLine();
            }
            bw.write(info);
            flag = true;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bw){
                    bw.close();
                    filew.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;

    }

    /**
     * 修改用户
     * @param userName 用户名
     * @param password 密码
     * @param userType 用户类型
     */
    private static boolean updateUser(String userName,String password,int userType,String gender,String education){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("user.txt");
        File file1 = new File("user_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        boolean flag = false;
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("user.txt");
            fileWriter = new FileWriter("user_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
//                找到要修改的数据,修改数据
                if (userName.equals(lines[1])){
                    line = userType+";"+userName+";"+password+";"+gender+";"+education;
                    flag = true;
                }
                bw.write(line);
                bw.newLine();
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("user.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * 删除用户
     * @param userName 用户名
     */
    private static boolean deleteUser(String userName){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        File file = new File("user.txt");
        File file1 = new File("user_bak.txt");
        boolean flag = false;

        try{

            if (!file.exists()){
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("user.txt");
            fileWriter = new FileWriter("user_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
//                删除指定数据，即不往新文件中写入
                if (userName.equals(lines[1])){
                    flag = true;
                    continue;
                }
                bw.write(line);
                bw.newLine();
            }

            file.delete();
            file1.renameTo(new File("user.txt"));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
                    file.delete();
                    file1.renameTo(new File("user.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    private static void workTimeSettingMenu(Scanner scanner){
        System.out.println("当前的工作时间设置为");
        HashMap<String,String> data = queryWorkTimeSetting();
        if (null == data){
            System.out.println("暂未设置");
        }else{
            System.out.println("上班时间:"+data.get("startTime")+";下班时间:"+data.get("endTime")+";每月允许迟到次数:"+data.get("lateCount")
                    +";每月允许早退次数:"+data.get("earlyCount"));
        }
        System.out.println("1.修改时间;4.返回上一层菜单;5.退出系统");
        int type = scanner.nextInt();
        switch (type){
            case 1:updateWorkTimeSettingMenu(scanner);
            case 4:menuSelect(scanner);
            case 5:System.exit(0);
        }
    }

    private static HashMap<String,String> queryWorkTimeSetting(){
        FileReader fileReader = null;
        BufferedReader bf = null;
        HashMap<String,String> data = new HashMap<>();
        try{
//            从文本文件中读取数据
            File file = new File("workTime.txt");
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("workTime.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                data.put("startTime",lines[0]);
                data.put("endTime",lines[1]);
//                允许迟到次数
                data.put("lateCount",lines[2]);
//                允许早退次数
                data.put("earlyCount",lines[3]);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }

    private static void updateWorkTimeSettingMenu(Scanner scanner){
        System.out.println("请输入上班时间");
        String startTime = scanner.next();
        System.out.println("请输入下班时间");
        String endTime = scanner.next();
        System.out.println("请输入允许迟到次数");
        int lateCount = scanner.nextInt();
        System.out.println("请输入允许早退次数");
        int earlyCount = scanner.nextInt();
        boolean result = updateWorkTimeSetting(startTime,endTime,lateCount,earlyCount);
        if (result){
            System.out.println("设置成功");
        }else{
            System.err.println("设置失败");
        }
        workTimeSettingMenu(scanner);
    }

    /**
     * 工作时间设置
     * @param startTime
     * @param endTime
     * @param lateCount
     * @param earlyCount
     * @return
     */
    private static boolean updateWorkTimeSetting(String startTime,String endTime,int lateCount,int earlyCount){
        FileWriter filew = null;
        BufferedWriter bw = null;
        boolean flag = false;
        try {
//            写入文件
            File file = new File("workTime.txt");
            if (file.exists()){
                file.delete();
                file.createNewFile();
                flag = true;
            }
            filew = new FileWriter(file,true);
            bw = new BufferedWriter(filew);
            if (file.length()==0){
                flag = true;
            }
            String info = startTime+";"+endTime+";"+lateCount+";"+earlyCount;
//            是否换行
            if (!flag){
                bw.newLine();
            }
            bw.write(info);
            flag = true;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bw){
                    bw.close();
                    filew.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }




    private static void attendanceMenu(Scanner scanner){
        System.out.println("1.上班打卡;2.下班打卡;4.返回上一层菜单;5.退出系统");
        int type = scanner.nextInt();
        switch (type){
            case 1:startWork();
            case 2:endWork();
            case 4:menuSelect(scanner);
            case 5:System.exit(0);
        }
    }
    private static SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");

    private static void startWork(){
        Date date = new Date();
        String format = dd.format(date);
        HashMap<String, String> stringStringHashMap = queryWorkTimeSetting();
        String startTime = stringStringHashMap.get("startTime");
        try {
            Date settingTime = sdf.parse(format+" "+startTime+":00");
            int i = date.compareTo(settingTime);
            boolean late = false;
            //已经迟到了
            if (i>0){
                late = true;
            }
            addWorkStatic(late);
            //记录打卡时间
            addWorkRecord(format,sdf.format(date),late);

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private static void endWork(){
        Date date = new Date();
        String format = dd.format(date);
        HashMap<String, String> stringStringHashMap = queryWorkTimeSetting();
        String startTime = stringStringHashMap.get("endTime");
        try {
            Date settingTime = sdf.parse(format+" "+startTime+":00");
            int i = date.compareTo(settingTime);
            boolean late = false;
            //已经迟到了
            if (i<0){
                late = true;
            }
            updateWorkStatic(getWorkTime(format,sdf.format(date)),late);
            //记录打卡时间
            updateWorkRecord(format,sdf.format(date),late);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static boolean addWorkRecord(String date,String time,boolean late){
        FileWriter filew = null;
        BufferedWriter bw = null;
        boolean flag = false;
        try {
//            写入文件
            File file = new File("workRecord.txt");
            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            filew = new FileWriter(file,true);
            bw = new BufferedWriter(filew);
            if (file.length()==0){
                flag = true;
            }
            String info = getCurrentUserName()+";"+date+";"+time+";"+"未打卡"+";正常;暂无记录";
            if (late){
                info = getCurrentUserName()+";"+date+";"+time+";"+"未打卡"+";迟到;暂无记录";
            }
//            是否换行
            if (!flag){
                bw.newLine();
            }
            bw.write(info);
            flag = true;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bw){
                    bw.close();
                    filew.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    private static void updateWorkRecord(String date,String time,boolean late){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("workRecord.txt");
        File file1 = new File("workRecord_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                return ;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("workRecord.txt");
            fileWriter = new FileWriter("workRecord_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
//                找到要修改的数据,修改数据
                if (getCurrentUserName().equals(lines[0]) && date.equals(lines[1])){
                    if (late){
                        line = getCurrentUserName()+";"+date+";"+lines[2]+";"+time+";"+lines[4]+";早退";
                    }else{
                        line = getCurrentUserName()+";"+date+";"+lines[2]+";"+time+";"+lines[4]+";正常";
                    }

                }
                bw.write(line);
                bw.newLine();
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("workRecord.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private static void myAttendanceMenu(Scanner scanner){
        System.out.println("1.查看记录;4.返回上一层菜单;5.退出系统");
        int type = scanner.nextInt();
        switch (type){
            case 1:
                System.out.println("请输入月份(如2020-02)");
                String month = scanner.next();
                List<HashMap<String, String>> hashMaps = queryWorkRecord(getCurrentUserName(),month);
                for (HashMap<String, String> hashMap : hashMaps) {
                    System.out.println("日期："+hashMap.get("date")+";上班时间:"+hashMap.get("startTime")+";下班时间:"
                            +hashMap.get("endTime")+";是否迟到:"
                            +hashMap.get("late")+";是否早退:"
                            +hashMap.get("early"));
                }
                break;
            case 4:menuSelect(scanner);break;
            case 5:System.exit(0);break;
            default:
        }
    }

    private static List<HashMap<String,String>> queryWorkRecord(String userName,String date){
        FileReader fileReader = null;
        BufferedReader bf = null;
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            从文本文件中读取数据
            File file = new File("workRecord.txt");
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("workRecord.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                if (line == ""){
                    continue;
                }
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                item.put("date",lines[1]);
                item.put("startTime",lines[2]);
                item.put("endTime",lines[3]);
                item.put("late",lines[4]);
                item.put("early",lines[5]);
//                如果筛选用户名
                if (userName != null){
                    if (!userName.equals(lines[0])){
                        continue;
                    }
                }
                if (date != null){
                    if (!(date.equals(lines[1].substring(0,7)))){
                        continue;
                    }
                }
                data.add(item);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }

    private static String getCurrentMonth(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        return simpleDateFormat.format(date);
    }

    private static void queryattendanceMenu(Scanner scanner){
        System.out.println("1.查看当月考勤记录;2.考勤异常人员信息统计;4.返回上一层菜单;5.退出系统");
        int type = scanner.nextInt();
        switch (type){
            case 1:
                List<HashMap<String, String>> hashMaps = queryWorkRecord(getCurrentUserName(),getCurrentMonth());
                if (null == hashMaps){
                    System.out.println("暂无记录");
                }else{
                    for (HashMap<String, String> hashMap : hashMaps) {
                        System.out.println("日期："+hashMap.get("date")+";上班时间:"+hashMap.get("startTime")+";下班时间:"
                                +hashMap.get("endTime")+";是否迟到:"
                                +hashMap.get("late")+";是否早退:"
                                +hashMap.get("early"));
                    }
                }
                break;
            case 2:
                List<HashMap<String, String>> data = queryWorkStatics(getCurrentUserName(),getCurrentMonth());
                if (null == data){
                    System.out.println("暂无记录");
                }else{
                    for (HashMap<String, String> hashMap : data) {
                        System.out.println("用户名："+hashMap.get("userName")+"日期："+hashMap.get("date")+";工作时长:"+hashMap.get("workTime")+";迟到次数:"
                                +hashMap.get("late")+";早退次数:"
                                +hashMap.get("early"));
                    }
                }

                break;
            case 4:menuSelect(scanner);break;
            case 5:System.exit(0);break;
            default:
        }
    }

    /**
     * 计算工时-一天的
     * @param date 日期
     * @param endTime 下班时间
     * @return
     */
    private static int getWorkTime(String date,String endTime){
        List<HashMap<String, String>> hashMaps = queryWorkRecord(getCurrentUserName(), date.substring(0,7));
        int time = 0;
        for (HashMap<String, String> hashMap : hashMaps) {
            if (date.equals(hashMap.get("date"))){
                try {
                    Date parse = sdf1.parse(hashMap.get("startTime"));
                    Date parse1 = sdf.parse(endTime);
                    time = parse1.getHours()-parse.getHours();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            }
        }
        return time;
    }

    /**
     * 记录工作次数
     */
    private static boolean addWorkStatic(boolean late){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("workStatics.txt");
        File file1 = new File("workStatics_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        boolean flag = false;
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                file.createNewFile();
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("workStatics.txt");
            fileWriter = new FileWriter("workStatics_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");

//                找到要修改的数据,修改数据
                if (getCurrentUserName().equals(lines[0])&&getCurrentMonth().equals(lines[1])){
                    if (late){
                        line = getCurrentUserName()+getCurrentMonth()+";"+lines[2]+";"+(Integer.parseInt(lines[3])+1)+";"+lines[4];
                    }else{
                        line = getCurrentUserName()+getCurrentMonth()+";"+lines[2]+";"+(Integer.parseInt(lines[3]))+";"+lines[4];
                    }

                    flag = true;
                }
                bw.write(line);
                bw.newLine();
            }
            if (!flag){
                if (late){
                    line = getCurrentUserName()+getCurrentMonth()+";"+0+";"+1+";"+0;
                }else{
                    line = getCurrentUserName()+getCurrentMonth()+";"+0+";"+0+";"+0;
                }

                bw.write(line);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("workStatics.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * 记录工作次数
     */
    private static boolean updateWorkStatic(int workTime,boolean early){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("workStatics.txt");
        File file1 = new File("workStatics_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        boolean flag = false;
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                file.createNewFile();
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("workStatics.txt");
            fileWriter = new FileWriter("workStatics_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");

//                找到要修改的数据,修改数据
                if (getCurrentUserName().equals(lines[0])&&getCurrentMonth().equals(lines[1])){
                    if (early){
                        line = getCurrentUserName()+";"+getCurrentMonth()+(Integer.parseInt(lines[2])+workTime)+";"+lines[3]+";"+(Integer.parseInt(lines[4])+1);
                    }else{
                        line = getCurrentUserName()+";"+getCurrentMonth()+(Integer.parseInt(lines[2])+workTime)+";"+lines[3]+";"+(Integer.parseInt(lines[4]));
                    }

                    flag = true;
                }
                bw.write(line);
                bw.newLine();
            }
            if (!flag){
                line = getCurrentUserName()+getCurrentMonth()+";"+0+";"+1+";"+0;
                bw.write(line);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("workStatics.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * 查询考勤统计
     * @param month
     * @param userName
     * @return
     */
    private static List<HashMap<String,String>> queryWorkStatics(String userName,String month){
        FileReader fileReader = null;
        BufferedReader bf = null;
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            从文本文件中读取数据
            File file = new File("workStatics.txt");
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("workStatics.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                if (line == ""){
                    continue;
                }
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                item.put("userName",lines[0]);
                item.put("date",lines[1]);
                item.put("workTime",lines[2]);
                item.put("late",lines[3]);
                item.put("early",lines[4]);
//                如果筛选用户名
                if (userName != null){
                    if (!userName.equals(lines[0])){
                        continue;
                    }
                }
                if (month != null){
                    if (!(month.equals(lines[1]))){
                        continue;
                    }
                }
                data.add(item);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }



















}
